public class Solution {
    public static int maxSum = Integer.MIN_VALUE;
    public static int maxPathSum(TreeNode root) {
        maxPathSumHelper(root);
        return maxSum;
    }
    public static int maxPathSumHelper(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftValue = Math.max(maxPathSumHelper(root.left), 0);
        int rightValue = Math.max(maxPathSumHelper(root.right), 0);
        int curValue = leftValue + rightValue + root.val;

        maxSum = Math.max(maxSum, curValue);

        return root.val + Math.max(leftValue, rightValue);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        System.out.println(maxPathSum(root));
    }
}
class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }